package com.dxd.servlet.admin;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dxd.constant.ErrorCodeConstant;
import com.dxd.entity.MessageType;
import com.dxd.entity.PageEntity;
import com.dxd.entity.ReturnBean;
import com.dxd.servlet.PublicServlet;

/**
 * 类型管理
 * 
 * @author pppcar
 *
 */
@WebServlet("/auth/admin/messageType")
public class MessageTypeServlet extends PublicServlet {
	private static final long serialVersionUID = -1093647922092449357L;
	private static final Logger LOG = LoggerFactory.getLogger(MessageTypeServlet.class);

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = request.getParameter("type");
		if ("typeList".equals(type)) {// 查询所有的用户，包括分页，查询条件
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			try {
				String name = request.getParameter("name");// 类型名称
				String messageType = request.getParameter("message_type");// 类型:1：消息分类，2：消息类型
				Integer mType = null;
				if (messageType != null && !"".equals(messageType)) {
					mType = Integer.parseInt(messageType);
				}
				PageEntity<MessageType> data = messageTypeDao.queryList(pageMap.get("page"), pageMap.get("size"), name,
						mType);
				returnBean.setData(data);
			} catch (Exception e) {
				LOG.error("查询信息类型失败，{}，{}", e.getMessage(), e);
				returnBean.setReturnCode(ErrorCodeConstant.ERROR).setReturnMsg("查询信息类型失败").setData(null);
			}
			returnJson(returnBean);
			return;
		} else if ("goMessageTypeList".equals(type)) {
			Dispatcher("/view/admin/message_type_list.jsp");
		} else if ("addMessageType".equals(type)) {// 添加类型
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String messageType = request.getParameter("message_type");
			String message_name = request.getParameter("message_name");
			String id = request.getParameter("id");
			if (messageType == null || "".equals(messageType)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请选择类型");
				returnJson(returnBean);
				return;
			}
			if (message_name == null || "".equals(message_name)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("请填写类型名称");
				returnJson(returnBean);
				return;
			}
			// 验证类型是否存在
			MessageType msgType = messageTypeDao.queryByName(message_name, messageType);
			if (msgType != null && msgType.getId() != null) {
				returnBean.setReturnCode(ErrorCodeConstant.ERROR).setReturnMsg("该类型已经存在");
				returnJson(returnBean);
				return;
			}
			msgType = new MessageType();
			msgType.setName(message_name);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			msgType.setCreateTime(format.format(new Date()));
			msgType.setType(2);
			msgType.setId(id == null ? null : Integer.parseInt(id));
			MessageType parent = new MessageType();
			parent.setId(Integer.parseInt(messageType));
			msgType.setParent(parent);

			if (id == null || "".equals(id)) {
				messageTypeDao.addMessageType(msgType);
				LOG.info("添加类型成功，名称：{}", message_name);
			} else {
				messageTypeDao.updateMessageType(msgType);
				LOG.info("修改类型成功，名称：{}", message_name);
			}

			returnJson(returnBean);
			return;

		} else if ("deleteMessageType".equals(type)) {
			ReturnBean returnBean = ReturnBean.returnBeanBuild();
			String ids = request.getParameter("ids");
			if (ids == null || "".equals(ids)) {
				returnBean.setReturnCode(ErrorCodeConstant.PARAM).setReturnMsg("参数为空");
				returnJson(returnBean);
				return;
			}
			messageTypeDao.deleteByUserIds(ids);
			LOG.info("删除类型成功，类型id：{}", ids);
			returnJson(returnBean);
			return;
		}
	}

}